﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace Eniux.Windows.ExtensionMethods
{
    /// <summary>
    /// DrawingContext扩展方法
    /// </summary>
    public static class DrawingContextExtensions
    {
        #region 公共方法

        /// <summary>
        /// 绘制弧形
        /// </summary>
        /// <param name="dc">绘制上下文DrawingContext</param>
        /// <param name="brush">笔刷</param>
        /// <param name="pen">笔</param>
        /// <param name="start">开始点</param>
        /// <param name="end">结束点</param>
        /// <param name="direction">椭圆弧的绘制方向</param>
        /// <param name="radiusX">x轴向弧度</param>
        /// <param name="radiusY">Y轴向弧度</param>
        public static void DrawArc(
            this DrawingContext dc,
            Brush brush,
            Pen pen,
            Point start,
            Point end,
            SweepDirection direction,
            double radiusX,
            double radiusY)
        {
            // setup the geometry object
            var geometry = new PathGeometry();
            var figure = new PathFigure();
            geometry.Figures.Add(figure);
            figure.StartPoint = start;

            // add the arc to the geometry
            figure.Segments.Add(new ArcSegment(end, new Size(radiusX, radiusY), 0, false, direction, true));

            // draw the arc
            dc.DrawGeometry(brush, pen, geometry);
        }

        /// <summary>
        /// 绘制弧形
        /// </summary>
        /// <param name="dc">绘制上下文DrawingContext</param>
        /// <param name="brush">笔刷</param>
        /// <param name="pen">笔</param>
        /// <param name="position">位置</param>
        /// <param name="startAngle">开始角度</param>
        /// <param name="endAngle">结束角度</param>
        /// <param name="direction">椭圆弧的绘制方向</param>
        /// <param name="radiusX">x轴向弧度</param>
        /// <param name="radiusY">y轴向弧度</param>
        public static void DrawArc(
            this DrawingContext dc,
            Brush brush,
            Pen pen,
            Point position,
            double startAngle,
            double endAngle,
            SweepDirection direction,
            double radiusX,
            double radiusY)
        {
            double startRadians = startAngle / 180 * Math.PI;
            double endRadians = endAngle / 180 * Math.PI;
            var start = position + new Vector(Math.Cos(startRadians) * radiusX, -Math.Sin(startRadians) * radiusY);
            var end = position + new Vector(Math.Cos(endRadians) * radiusX, -Math.Sin(endRadians) * radiusY);
            dc.DrawArc(brush, pen, start, end, direction, radiusX, radiusY);
        }

        /// <summary>
        /// 绘制弧形
        /// </summary>
        /// <param name="dc">绘制上下文</param>
        /// <param name="brush">笔刷</param>
        /// <param name="pen">笔</param>
        /// <param name="position">位置</param>
        /// <param name="startAngle">开始角度</param>
        /// <param name="endAngle">结束角度</param>
        /// <param name="radiusX">x轴向弧度</param>
        /// <param name="radiusY">y轴向弧度</param>
        public static void DrawArc(
            this DrawingContext dc,
            Brush brush,
            Pen pen,
            Point position,
            double startAngle,
            double endAngle,
            double radiusX,
            double radiusY)
        {
            DrawArc(dc, brush, pen, position, startAngle, endAngle, SweepDirection.Counterclockwise, radiusX, radiusY);
        }

        #endregion
    }
}
